import copy
import celery


def easy_apply_async(args=None, kwargs=None, task_id=None, producer=None,
                     link=None, link_error=None, shadow=None,
                     countdown=None,
                     eta=None, connection=None,
                     router=None, expires=None,
                     publisher=None,
                     add_to_parent=True, group_id=None, retries=0, chord=None,
                     reply_to=None, time_limit=None, soft_time_limit=None,
                     root_id=None, parent_id=None, route_name=None,
                     chain=None, task_function=None, **options):
    """
    具有强大的自动补全功能，可以补全提示20种常用的任务控制参数，celery原生不支持。

    :param args:
    :param kwargs:
    :param task_id:
    :param producer:
    :param link:
    :param link_error:
    :param shadow:
    :param countdown:
    :param eta:
    :param connection:
    :param router:
    :param expires:
    :param publisher:
    :param add_to_parent:
    :param group_id:
    :param retries:
    :param chord:
    :param reply_to:
    :param time_limit:
    :param soft_time_limit:
    :param root_id:
    :param parent_id:
    :param route_name:
    :param chain:
    :param task_function:
    :param options:
    :return:
    """
    locals_copy = copy.copy(locals())
    if not task_function or hasattr(task_function, 'apply_async') is False:
        raise ValueError(f'task_function 必须传一个被shared_task 或app.task装饰的函数任务')
    locals_copy.pop('task_function')
    options_copy = options
    locals_copy.update(options_copy)
    locals_copy.pop('options')
    # print(celery.current_app.conf.task_routes)
    # print(celery.current_app)
    if celery.current_app.conf.task_routes is None:
        celery.current_app.conf.task_routes = {}
    celery.current_app.conf.task_routes.update({task_function.name: {'queue': task_function.name}})
    return task_function.apply_async(**locals_copy)


def easy_delay(*args, task_function=None, **kwargs):
    if not task_function or hasattr(task_function, 'apply_async') is False:
        raise ValueError(f'task_function 必须传一个被shared_task 或app.task装饰的函数任务')
    return easy_apply_async(args=args, kwargs=kwargs, task_function=task_function)
